package com.ardf.controller;

import com.ardf.common.EntityConstants;
import com.ardf.entity.AthleteTeam;
import com.ardf.entity.Group;
import com.ardf.entity.dto.TeamGroupDto;
import com.ardf.service.IAthleteTeamService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author ShiYunHao
 * @since 2025-01-06
 */
@RestController
@RequestMapping("/ardf/athleteTeam")
public class AthleteTeamController {

    private final IAthleteTeamService athleteTeamService;

    public AthleteTeamController(IAthleteTeamService athleteTeamService) {
        this.athleteTeamService = athleteTeamService;
    }

    /**
     * 获取接力团队的组别
     */
    @GetMapping("/getGroupByTeamId")
    public ResponseEntity<TeamGroupDto> getGroupByTeamId(@RequestParam("teamId") Long teamId) {
        Group group = athleteTeamService.getGroupByTeamId(teamId);
        TeamGroupDto teamGroupDto = new TeamGroupDto();
        teamGroupDto.setGroup(group);
        return ResponseEntity.ok(teamGroupDto);
    }

    /**
     * 根据选手id获取接力团队的非替补人员的个数
     */
    @GetMapping("/checkCountByAthleteId")
    public ResponseEntity<String> checkCountByAthleteId(@RequestParam("athleteId") Long athleteId) {
        AthleteTeam athleteTeam = athleteTeamService.getOne(new LambdaQueryWrapper<AthleteTeam>()
                .eq(AthleteTeam::getAthleteId, athleteId));
        long count = athleteTeamService.count(new LambdaQueryWrapper<AthleteTeam>()
                .eq(AthleteTeam::getSubstitute, EntityConstants.ATHLETETEAM_SUBSTITUTE_NO)
                .in(AthleteTeam::getTeamId, athleteTeam.getTeamId()));
        if(count>=3&& EntityConstants.ATHLETETEAM_SUBSTITUTE_YES.equals(athleteTeam.getSubstitute())){
            return ResponseEntity.ok("no");
        }else {
            return ResponseEntity.ok("yes");
        }
    }

}
